package com.zeroxml.cms.web.action.cms;

import com.zeroxml.cms.web.action.BaseAction;
import com.zeroxml.cms.bo.Article;
import com.zeroxml.cms.bo.Comment;
import com.zeroxml.cms.bo.User;
import com.zeroxml.cms.service.IArticleService;
import com.zeroxml.cms.service.ICommentService;
import com.zeroxml.cms.service.IUserService;
import com.zeroxml.cms.exception.ArticleNotExistException;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.Date;
import java.util.List;

/**
 * User: cjp
 * Date: 2007-9-22
 * Time: 7:19:36
 */
@UrlBinding("/article.do")
public class ArticleAction extends BaseAction {
    private long id;
    private Article article;
    @SpringBean
    private IArticleService articleService;
    @SpringBean
    private ICommentService commentService;
    private HibernatePage<Comment> comments;
    @SpringBean
    private IUserService userService;
    private Comment comment;
    private int curPage;
    private List<Article> recommedArticles;

    public Article getArticle() {
        return article;
    }

    public void setId(long id) {
        this.id = id;
    }

    public List<Article> getRecommedArticles() {
        return recommedArticles;
    }

    public void setArticleService(IArticleService articleService) {
        this.articleService = articleService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setCommentService(ICommentService commentService) {
        this.commentService = commentService;
    }

    public HibernatePage<Comment> getComments() {
        return comments;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public Comment getComment() {
        return comment;
    }

    public void setComment(Comment comment) {
        this.comment = comment;
    }

    @DefaultHandler
    public Resolution viewArticle(){
        article=articleService.getArticle(id);
        if(article==null){
            throw new ArticleNotExistException();
        }
        article.setReadSize(article.getReadSize()+1);
        articleService.updateArticle(article);
        int pageSize=10;
        recommedArticles = articleService.getRecommedArticles(article.getCatalog().getId());
        comments=commentService.getComments(id,curPage,pageSize);
        return getCmsResolution("article/viewArticle");
    }

    public Resolution commentArticle(){
        long userId=getUserId();
        if(userId<=0){
            userId=1;
        }
        User user=userService.getUser(userId);

        Article article=articleService.getArticle(comment.getArticle().getId());
        if(article==null){
            throw new ArticleNotExistException();
        }
        article.setCommentSize(article.getCommentSize()+1);
        articleService.updateArticle(article);
        String ip=getRequest().getRemoteAddr();

        comment.setPostIp(ip);
        comment.setCreator(user);
        comment.setCreateTime(new Date());
        commentService.doComment(comment);
        return new RedirectResolution(getClass()).addParameter("id",article.getId());
    }
}
